package com.bnlr.lrparking.appui.mine.recentparkrecord;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.LogUtils;
import com.bnlr.lrparking.R;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * @author: Licy
 * date: 2018/10/26
 * description: TODO
 */
public class ReceiptGridViewAdapter extends BaseAdapter {

    private Context mContext;
    private List<String> mData;

    public ReceiptGridViewAdapter(Context mContext, List<String> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }


    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        LogUtils.d("width-display :" + dm.widthPixels);
        LogUtils.d("height-display :" + dm.heightPixels);

        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_receipt_img, null);
            viewHolder = new ViewHolder();
            viewHolder.receipt = (ImageView) view.findViewById(R.id.item_receipt_img);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        int imageWidth = (dm.widthPixels - 24 - 30) / 3;
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) viewHolder.receipt.getLayoutParams();
        layoutParams.width = imageWidth;
        layoutParams.height = imageWidth;
        viewHolder.receipt.setLayoutParams(layoutParams);

        Glide.with(mContext)
                .load(mData.get(position))
                .into(viewHolder.receipt);

        return view;
    }


    private class ViewHolder {
        ImageView receipt;
    }

}
